<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="20%"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="auto"
    >
      <el-form-item label="班级" prop="classId">
        <el-select v-model="dataForm.classId" filterable placeholder="请选择班级">
          <el-option
            v-for="item in classList"
            :key="item.classId"
            :label="item.className"
            :value="item.classId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="courseId">
        <el-select v-model="dataForm.courseId" filterable placeholder="请选择课程">
          <el-option
            v-for="item in courseList"
            :key="item.courseId"
            :label="item.courseName"
            :value="item.courseId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="教师" prop="teacherId">
        <el-select v-model="dataForm.teacherId" filterable placeholder="请选择教师">
          <el-option
            v-for="item in teacherList"
            :key="item.teacherId"
            :label="item.teacherName"
            :value="item.teacherId"
          >
            <span style="float: left">{{ item.teacherName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userName }}</span>
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        classId: '',
        courseId: '',
        teacherId: ''
      },
      classList: [],
      courseList: [],
      teacherList: [],
      dataRule: {
        classId: [
          { required: true, message: '班级ID不能为空', trigger: 'blur' }
        ],
        courseId: [
          { required: true, message: '课程ID不能为空', trigger: 'blur' }
        ],
        teacherId: [
          { required: true, message: '教师ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getSelections () {
      this.$http({
        url: this.$http.adornUrl(`/workmanagement/classcourse/getSelecttions`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.classList = data.classCourse.classList
          this.courseList = data.classCourse.courseList
          this.teacherList = data.classCourse.teacherList
        }
      })
    },
    init (id) {
      this.getSelections()
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/workmanagement/classcourse/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.classId = data.classCourse.classId
              this.dataForm.courseId = data.classCourse.courseId
              this.dataForm.teacherId = data.classCourse.teacherId
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/workmanagement/classcourse/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'classId': this.dataForm.classId,
              'courseId': this.dataForm.courseId,
              'teacherId': this.dataForm.teacherId
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
